using System;

namespace TrueCode.DependencyManager.BuildActions.Instantiating
{
  public class FactoryMethod : IInstantiate
  {
    private readonly Func< IBuildSession, object > _factoryMethod;

    public FactoryMethod( Func< IBuildSession, object > factoryMethod )
    {
      _factoryMethod = factoryMethod;
    }

    #region Implementation of IBuildAction
    public void Execute( IBuildSession context )
    {
      context.Instance = _factoryMethod( context );
      context.ExecuteNextBuildAction();
    }
    #endregion
  }

  public class FactoryMethod< T > : IInstantiate
  {
    private readonly Func< IBuildSession, T > _factoryMethod;

    public FactoryMethod( Func< IBuildSession, T > factoryMethod )
    {
      _factoryMethod = factoryMethod;
    }

    #region Implementation of IBuildAction
    public void Execute( IBuildSession context )
    {
      context.Instance = _factoryMethod( context );
      context.ExecuteNextBuildAction();
    }
    #endregion
  }

  public class SimpleFactoryMethod< T > : IInstantiate
  {
    private readonly Func< T > _factoryMethod;

    public SimpleFactoryMethod( Func< T > factoryMethod )
    {
      _factoryMethod = factoryMethod;
    }

    #region Implementation of IBuildAction
    public void Execute( IBuildSession context )
    {
      context.Instance = _factoryMethod();
      context.ExecuteNextBuildAction();
    }
    #endregion
  }
}